package com.xand.bt;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xand.bt.api.MarketConnector;
import com.xand.bt.api.MarketImageChangeNotify;
import com.xand.bt.market.MarketStoresManager;

public class MarketStoreUpdateThread implements Runnable {

	private static final Logger logger = LoggerFactory.getLogger(MarketStoreUpdateThread.class);
	
	private Thread thread;
	
	private MarketConnector connector;
	
	private boolean running = false;
	
	public MarketStoreUpdateThread(String marketLabel, MarketConnector marketConnector) {
		thread = new Thread(this, marketLabel + "_THREAD");
		
		this.connector = marketConnector;
	}
	
	public void start() {
		
		this.running = true;
		this.thread.start();
		
	}
	
	public void run() {
		
		while(this.running) {
			
			MarketStoresManager marketStoresManager = MarketStoresManager.getInstance();
			
			List<MarketImageChangeNotify> changes = this.connector.getMarketImageChanges();
			
			for(MarketImageChangeNotify singleChange : changes) {
				
				marketStoresManager.notifyMarketImageChange(singleChange);
				
			}
			
			try {
				Thread.sleep(1000);
			}catch(Exception e){
				
				logger.error("An error occured", e);
				throw new RuntimeException(e);
				
			}
			
		}
		
	}
}
